package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mikazuki on 2025/3/28.
 */
@Repository
public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;
    @Override
    public Integer insert(User user) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "insert into user(username, password, nickname, age) values(?, ?, ?, ?)";
            PreparedStatement statement = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            statement.setString(1, user.getUsername());
            statement.setString(2, user.getPassword());
            statement.setString(3, user.getNickname());
            statement.setInt(4, user.getAge());

            int num = statement.executeUpdate();
            // 获取新增之后的数据Id
            if (num > 0) {
                ResultSet resultSet = statement.getGeneratedKeys();
                if (resultSet.next()) {
                    int id = resultSet.getInt(1);
                    System.out.println("注册成功，用户Id为:"+ id);
                    return id;
                }
            }
            System.out.println("注册失败");
        } catch (SQLException e) {
            System.out.println("注册失败");
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<User> select(User user) {
        try (Connection connection = dbUtil.getConnection()) {
            // 按照user类对象中不为空的字段拼接查询sql
            StringBuilder sb = new StringBuilder("select * from user where 1=1");
            if (user.getUsername() != null) {
                sb.append(" and username = ?");
            }
            if (user.getPassword() != null) {
                sb.append(" and password = ?");
            }
            if (user.getNickname() != null) {
                sb.append(" and nickname = ?");
            }
            if (user.getAge() != null) {
                sb.append(" and age = ?");
            }
            if (user.getId() != null) {
                sb.append(" and id = ?");
            }
            // 根据user类对象中不为空的字段拼接查询sql后的sql来插入sql中预设的值
            PreparedStatement statement = connection.prepareStatement(sb.toString());
            int index = 1;
            if (user.getUsername() != null) {
                statement.setString(index++, user.getUsername());
            }
            if (user.getPassword() != null) {
                statement.setString(index++, user.getPassword());
            }
            if (user.getNickname() != null) {
                statement.setString(index++, user.getNickname());
            }
            if (user.getAge() != null) {
                statement.setInt(index++, user.getAge());
            }
            if (user.getId() != null) {
                statement.setInt(index, user.getId());
            }
            ResultSet resultSet = statement.executeQuery();
            List<User> list = new ArrayList<>();
            while (resultSet.next()) {
                User user1 = new User();
                user1.setId(resultSet.getInt("id"));
                user1.setUsername(resultSet.getString("username"));
                user1.setPassword(resultSet.getString("password"));
                user1.setNickname(resultSet.getString("nickname"));
                user1.setAge(resultSet.getInt("age"));
                list.add(user1);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer update(User user) {
        try (Connection connection = dbUtil.getConnection()) {
            // 按照user类对象中不为空的字段拼接查询sql
            StringBuilder sb = new StringBuilder("update user set ");
            if (user.getUsername() != null) {
                sb.append(" username = ?");
            }
            if (user.getPassword() != null) {
                sb.append(" ,password = ?");
            }
            if (user.getNickname() != null) {
                sb.append(" ,nickname = ?");
            }
            if (user.getAge() != null) {
                sb.append(" ,age = ?");
            }
            if (user.getId() != null) {
                sb.append(" where id = ?");
            }
            // 根据user类对象中不为空的字段拼接查询sql后的sql来插入sql中预设的值
            PreparedStatement statement = connection.prepareStatement(sb.toString());
            int index = 1;
            if (user.getUsername() != null) {
                statement.setString(index++, user.getUsername());
            }
            if (user.getPassword() != null) {
                statement.setString(index++, user.getPassword());
            }
            if (user.getNickname() != null) {
                statement.setString(index++, user.getNickname());
            }
            if (user.getAge() != null) {
                statement.setInt(index++, user.getAge());
            }
            if (user.getId() != null) {
                statement.setInt(index, user.getId());
            }
            int num = statement.executeUpdate();
            return num;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer delete(User user) {
        try (Connection connection = dbUtil.getConnection()) {
            // 按照user类对象中不为空的字段拼接查询sql
            StringBuilder sb = new StringBuilder("delete from user where id = ?");
            // 根据user类对象中不为空的字段拼接查询sql后的sql来插入sql中预设的值
            PreparedStatement statement = connection.prepareStatement(sb.toString());
            statement.setInt(1, user.getId());
            int num = statement.executeUpdate();
            return num;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
